package org.test.mail;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件服务
 *
 * @author wanglei
 * @date 2021/10/25
 */
@Service
public class MailService {

    @Resource
    private JavaMailSender mailSender;

    //邮件发送方
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 简单文本邮件
     *
     * @param to      收件人
     * @param title   标题
     * @param content 内容
     */
    public void sendSimpleMail(String to, String title, String content) {
        //防止文件名称过长导致被截取情况发生
        System.setProperty("mail.mime.splitlongparameters", "false");
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(title);
        //邮件内容
        message.setText(content);
        //发送邮件
        mailSender.send(message);
    }

    /**
     * html邮件
     *
     * @param to      收件人
     * @param title   标题
     * @param content 内容
     */
    public void sendHtmlMail(String to, String title, String content) {
        //防止文件名称过长导致被截取情况发生
        System.setProperty("mail.mime.splitlongparameters", "false");
        //获取MimeMessage对象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(to);
            //邮件主题
            message.setSubject(title);
            //邮件内容，html格式
            messageHelper.setText(content, true);
            //发送
            mailSender.send(message);
            //日志信息
            System.out.println("邮件发送成功。");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 带附件的邮件
     *
     * @param to       收件人
     * @param title    标题
     * @param content  内容
     * @param filePath 附件
     */
    public void sendAttachmentsMail(String to, String title, String content, String filePath) {
        //防止文件名称过长导致被截取情况发生
        System.setProperty("mail.mime.splitlongparameters", "false");
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //设置编码格式
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(title);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            System.out.println("邮件发送成功。");
        } catch (MessagingException e) {
            System.out.println("发送邮件时发生异常！");
        }
    }
}
